package com.basics.common.utils.logistics;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 快递工具
 */
public class LogisticsUtils {

    public static HashMap<String,String> LogisticsMap = new HashMap<>();

    private final static String appCode = "af2a0deb5ddf434aa331cfa2eb3e1884";
    public final static String logisticsStatus = "SIGN";
    private final static String host = "https://express3.market.alicloudapi.com";
    private final static String path = "/express3";
    public static JSONObject getLogisticsInfo(String logisticsNo,String logisticsName,String phone){
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("number", logisticsNo);
        if(LogisticsMap.containsKey(logisticsName)){
            querys.put("type",LogisticsMap.get(logisticsName));
            if(LogisticsMap.get(logisticsName).equals("SF")){
                querys.put("mobile",phone.substring(phone.length()-4));
            }
        }

        try{
            HttpResponse response = HttpUtils.doGet(host, path, "GET", headers, querys);
            JSONObject jo = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            return jo.getJSONObject("data").getJSONArray("info").getJSONObject(0);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    static{
        LogisticsMap.put("圆通快递","YTO");
        LogisticsMap.put("韵达快递","YUNDA");
        LogisticsMap.put("中通快递","ZTO");
        LogisticsMap.put("中通快运","ZTOKY");
        LogisticsMap.put("申通快递","STO");
        LogisticsMap.put("百世快递","HTKY");
        LogisticsMap.put("百世快运","BESTQJT");
        LogisticsMap.put("极兔快递","JT");
        LogisticsMap.put("顺丰速运","SF");
        LogisticsMap.put("丰网速运","FW");
        LogisticsMap.put("中国邮政","EMS");
        LogisticsMap.put("京东快递","JD");
        LogisticsMap.put("跨越速运","KYE");
        LogisticsMap.put("德邦快递","DBKD");
        LogisticsMap.put("信丰快递","XFWL");
        LogisticsMap.put("优速快递","UC");
        LogisticsMap.put("天天快递","TTKDEX");
        LogisticsMap.put("宅急送","ZJS");
        LogisticsMap.put("安能","ANE56");
        LogisticsMap.put("快捷","FAST");
        LogisticsMap.put("德邦物流","DBL");
    }
    public static void main(String[] args) {
       System.out.println(getLogisticsInfo("773329907738602","申通快递",""));
    }
}
